{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "43f9ce31",
      "metadata": {
        "id": "43f9ce31"
      },
      "source": [
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pinecone-io/examples/blob/master/docs/quick-tour/namespacing.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/pinecone-io/examples/blob/master/docs/quick-tour/namespacing.ipynb)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "artificial-devil",
      "metadata": {
        "id": "artificial-devil",
        "papermill": {
          "duration": 0.037678,
          "end_time": "2021-04-16T15:12:08.268491",
          "exception": false,
          "start_time": "2021-04-16T15:12:08.230813",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "# Namespacing with Pinecone\n",
        "\n",
        "Namespacing is a neat feature in a Pinecone service that allows you to partition your data in an index. When you read from or write to a namespace in an index, you only access data in that particular namespace. In other words, data from two namespaces may have the same ids but different values. Namespacing is useful when you want to reuse the same data processing pipeline but query only a subset of your data.\n",
        "For example, when you are building a movie recommender system, you could use namespacing to separate recommendations by genre. Another way is to use metadata instead of namespaces for recommendations by genre."
      ]
    },
    {
      "cell_type": "markdown",
      "id": "atmospheric-wayne",
      "metadata": {
        "id": "atmospheric-wayne",
        "papermill": {
          "duration": 0.028014,
          "end_time": "2021-04-16T15:12:08.327699",
          "exception": false,
          "start_time": "2021-04-16T15:12:08.299685",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "## Prerequisites\n",
        "\n",
        "A paid Pinecone account as namespaces are not available on the free starter tier."
      ]
    },
    {
      "cell_type": "markdown",
      "id": "emotional-lyric",
      "metadata": {
        "id": "emotional-lyric",
        "papermill": {
          "duration": 0.027173,
          "end_time": "2021-04-16T15:12:08.383073",
          "exception": false,
          "start_time": "2021-04-16T15:12:08.355900",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "Install dependencies."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "pleasant-transfer",
      "metadata": {
        "execution": {
          "iopub.execute_input": "2021-04-16T15:12:08.445484Z",
          "iopub.status.busy": "2021-04-16T15:12:08.444722Z",
          "iopub.status.idle": "2021-04-16T15:12:24.290196Z",
          "shell.execute_reply": "2021-04-16T15:12:24.292389Z"
        },
        "id": "pleasant-transfer",
        "papermill": {
          "duration": 15.880968,
          "end_time": "2021-04-16T15:12:24.293137",
          "exception": false,
          "start_time": "2021-04-16T15:12:08.412169",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [],
      "source": [
        "!pip install -qU \\\n",
        "  pinecone-client==3.1.0 \\\n",
        "  pandas==2.0.3"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "moderate-distributor",
      "metadata": {
        "id": "moderate-distributor",
        "papermill": {
          "duration": 0.026427,
          "end_time": "2021-04-16T15:12:24.354705",
          "exception": false,
          "start_time": "2021-04-16T15:12:24.328278",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "Set up Pinecone."
      ]
    },
    {
      "cell_type": "markdown",
      "id": "08e296e2",
      "metadata": {},
      "source": [
        "Before getting started, decide whether to use serverless or pod-based index."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e6b4e8b2",
      "metadata": {},
      "outputs": [],
      "source": [
        "import os\n",
        "\n",
        "use_serverless = os.environ.get(\"USE_SERVERLESS\", \"False\").lower() == \"true\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Creating an Index\n",
        "\n",
        "Now the data is ready, we can set up our index to store it.\n",
        "\n",
        "We begin by initializing our connection to Pinecone. To do this we need a [free API key](https://app.pinecone.io)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from pinecone import Pinecone\n",
        "\n",
        "# initialize connection to pinecone (get API key at app.pc.io)\n",
        "api_key = os.environ.get('PINECONE_API_KEY') or 'PINECONE_API_KEY'\n",
        "environment = os.environ.get('PINECONE_ENVIRONMENT') or 'PINECONE_ENVIRONMENT'\n",
        "\n",
        "# configure client\n",
        "pc = Pinecone(api_key=api_key)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now we setup our index specification, this allows us to define the cloud provider and region where we want to deploy our index. You can find a list of all [available providers and regions here](https://docs.pinecone.io/docs/projects)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from pinecone import ServerlessSpec, PodSpec\n",
        "\n",
        "if use_serverless:\n",
        "    cloud = os.environ.get('PINECONE_CLOUD') or 'PINECONE_CLOUD'\n",
        "    spec = ServerlessSpec(cloud='aws', region='us-west-2')\n",
        "else:\n",
        "    spec = PodSpec(environment=environment)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "UYgB1gef1Utk",
      "metadata": {
        "id": "UYgB1gef1Utk",
        "tags": [
          "parameters"
        ]
      },
      "outputs": [],
      "source": [
        "index_name = \"pinecone-namespacing\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "balanced-housing",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-16T15:12:25.428720Z",
          "iopub.status.busy": "2021-04-16T15:12:25.427975Z",
          "iopub.status.idle": "2021-04-16T15:12:41.453928Z",
          "shell.execute_reply": "2021-04-16T15:12:41.453045Z"
        },
        "id": "balanced-housing",
        "outputId": "8a0bcad7-ed5b-4fd5-897e-1534497aa278",
        "papermill": {
          "duration": 16.057888,
          "end_time": "2021-04-16T15:12:41.454202",
          "exception": false,
          "start_time": "2021-04-16T15:12:25.396314",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "['arxiv-test',\n",
              " 'azure-openai-langchain-intro',\n",
              " 'llama-2-rag',\n",
              " 'openai-retrieval-test',\n",
              " 'pinecone-namespacing']"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "import time\n",
        "\n",
        "# Delete index if exists\n",
        "if index_name in pc.list_indexes().names():\n",
        "    pc.delete_index(index_name)\n",
        "\n",
        "# Create an index\n",
        "pc.create_index(\n",
        "    name=index_name, \n",
        "    dimension=2, \n",
        "    metric=\"euclidean\",\n",
        "    spec=spec\n",
        ")\n",
        "\n",
        "# wait for index to be ready before connecting\n",
        "while not pc.describe_index(index_name).status['ready']:\n",
        "    time.sleep(1)\n",
        "\n",
        "pc.list_indexes().names()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "blvbpzBAxPJO",
      "metadata": {
        "execution": {
          "iopub.execute_input": "2021-04-16T15:12:41.529466Z",
          "iopub.status.busy": "2021-04-16T15:12:41.528443Z",
          "iopub.status.idle": "2021-04-16T15:12:42.356838Z",
          "shell.execute_reply": "2021-04-16T15:12:42.357714Z"
        },
        "id": "blvbpzBAxPJO",
        "papermill": {
          "duration": 0.869129,
          "end_time": "2021-04-16T15:12:42.358177",
          "exception": false,
          "start_time": "2021-04-16T15:12:41.489048",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [],
      "source": [
        "# Connect to the index\n",
        "index = pc.Index(index_name)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "improved-season",
      "metadata": {
        "id": "improved-season",
        "papermill": {
          "duration": 0.038949,
          "end_time": "2021-04-16T15:12:42.437637",
          "exception": false,
          "start_time": "2021-04-16T15:12:42.398688",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "### Generate movie data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "disciplinary-district",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 175
        },
        "execution": {
          "iopub.execute_input": "2021-04-16T15:12:42.511608Z",
          "iopub.status.busy": "2021-04-16T15:12:42.510883Z",
          "iopub.status.idle": "2021-04-16T15:12:42.714783Z",
          "shell.execute_reply": "2021-04-16T15:12:42.715237Z"
        },
        "id": "disciplinary-district",
        "outputId": "25708b6c-92c4-4fd0-be92-dcd67d75a48d",
        "papermill": {
          "duration": 0.24115,
          "end_time": "2021-04-16T15:12:42.715499",
          "exception": false,
          "start_time": "2021-04-16T15:12:42.474349",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "\n",
              "  <div id=\"df-20e0304e-291c-49f3-ac5f-1f70a39d679c\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>id</th>\n",
              "      <th>vector</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>Wall-E</td>\n",
              "      <td>[1.0, 1.0]</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>Up</td>\n",
              "      <td>[2.0, 2.0]</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>Ratatouille</td>\n",
              "      <td>[3.0, 3.0]</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>Toy Story</td>\n",
              "      <td>[4.0, 4.0]</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-20e0304e-291c-49f3-ac5f-1f70a39d679c')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "\n",
              "\n",
              "\n",
              "    <div id=\"df-177dcdd9-a07c-4584-9941-3de6442b5c51\">\n",
              "      <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-177dcdd9-a07c-4584-9941-3de6442b5c51')\"\n",
              "              title=\"Suggest charts.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "      </button>\n",
              "    </div>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "    background-color: #E8F0FE;\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: #1967D2;\n",
              "    height: 32px;\n",
              "    padding: 0 0 0 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: #E2EBFA;\n",
              "    box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: #174EA6;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "    background-color: #3B4455;\n",
              "    fill: #D2E3FC;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart:hover {\n",
              "    background-color: #434B5C;\n",
              "    box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "    filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "    fill: #FFFFFF;\n",
              "  }\n",
              "</style>\n",
              "\n",
              "    <script>\n",
              "      async function quickchart(key) {\n",
              "        const containerElement = document.querySelector('#' + key);\n",
              "        const charts = await google.colab.kernel.invokeFunction(\n",
              "            'suggestCharts', [key], {});\n",
              "      }\n",
              "    </script>\n",
              "\n",
              "      <script>\n",
              "\n",
              "function displayQuickchartButton(domScope) {\n",
              "  let quickchartButtonEl =\n",
              "    domScope.querySelector('#df-177dcdd9-a07c-4584-9941-3de6442b5c51 button.colab-df-quickchart');\n",
              "  quickchartButtonEl.style.display =\n",
              "    google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "}\n",
              "\n",
              "        displayQuickchartButton(document);\n",
              "      </script>\n",
              "      <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-20e0304e-291c-49f3-ac5f-1f70a39d679c button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-20e0304e-291c-49f3-ac5f-1f70a39d679c');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "text/plain": [
              "            id      vector\n",
              "0       Wall-E  [1.0, 1.0]\n",
              "1           Up  [2.0, 2.0]\n",
              "2  Ratatouille  [3.0, 3.0]\n",
              "3    Toy Story  [4.0, 4.0]"
            ]
          },
          "execution_count": 6,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Generate some data\n",
        "import pandas as pd\n",
        "\n",
        "df = pd.DataFrame()\n",
        "df[\"id\"] = [\"Wall-E\", \"Up\", \"Ratatouille\", \"Toy Story\"]\n",
        "df[\"vector\"] = [[1., 1.], [2., 2.], [3., 3.], [4., 4.]]\n",
        "df"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "leading-flesh",
      "metadata": {
        "id": "leading-flesh",
        "papermill": {
          "duration": 0.030901,
          "end_time": "2021-04-16T15:12:42.777653",
          "exception": false,
          "start_time": "2021-04-16T15:12:42.746752",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "### Insert vectors without specifying a namespace"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "nearby-skiing",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-16T15:12:42.842654Z",
          "iopub.status.busy": "2021-04-16T15:12:42.841906Z",
          "iopub.status.idle": "2021-04-16T15:12:44.463755Z",
          "shell.execute_reply": "2021-04-16T15:12:44.458846Z"
        },
        "id": "nearby-skiing",
        "outputId": "db1c8819-86bd-49db-fadb-3f175938c1a1",
        "papermill": {
          "duration": 1.65623,
          "end_time": "2021-04-16T15:12:44.464926",
          "exception": false,
          "start_time": "2021-04-16T15:12:42.808696",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'dimension': 2,\n",
              " 'index_fullness': 0.0,\n",
              " 'namespaces': {'': {'vector_count': 4}},\n",
              " 'total_vector_count': 4}"
            ]
          },
          "execution_count": 7,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Insert vectors without specifying a namespace\n",
        "index.upsert(vectors=zip(df.id, df.vector))\n",
        "index.describe_index_stats()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "large-tunisia",
      "metadata": {
        "id": "large-tunisia",
        "papermill": {
          "duration": 0.033445,
          "end_time": "2021-04-16T15:12:44.537290",
          "exception": false,
          "start_time": "2021-04-16T15:12:44.503845",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "### Insert vectors into a namespace"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "excellent-channel",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 112
        },
        "execution": {
          "iopub.execute_input": "2021-04-16T15:12:44.610746Z",
          "iopub.status.busy": "2021-04-16T15:12:44.609058Z",
          "iopub.status.idle": "2021-04-16T15:12:44.612765Z",
          "shell.execute_reply": "2021-04-16T15:12:44.612174Z"
        },
        "id": "excellent-channel",
        "outputId": "7d790f07-b893-4384-abf6-78a7fe5bbad2",
        "papermill": {
          "duration": 0.043725,
          "end_time": "2021-04-16T15:12:44.612991",
          "exception": false,
          "start_time": "2021-04-16T15:12:44.569266",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "\n",
              "  <div id=\"df-119e686d-3a1c-4f9f-8f79-d45ba1e6118f\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>id</th>\n",
              "      <th>vector</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>Wall-E</td>\n",
              "      <td>[1.0, 1.0]</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>Ratatouille</td>\n",
              "      <td>[3.0, 3.0]</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-119e686d-3a1c-4f9f-8f79-d45ba1e6118f')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "\n",
              "\n",
              "\n",
              "    <div id=\"df-bf574b15-141a-4290-9a51-75b2c2a2c150\">\n",
              "      <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-bf574b15-141a-4290-9a51-75b2c2a2c150')\"\n",
              "              title=\"Suggest charts.\"\n",
              "              style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "      </button>\n",
              "    </div>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "    background-color: #E8F0FE;\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: #1967D2;\n",
              "    height: 32px;\n",
              "    padding: 0 0 0 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: #E2EBFA;\n",
              "    box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: #174EA6;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "    background-color: #3B4455;\n",
              "    fill: #D2E3FC;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart:hover {\n",
              "    background-color: #434B5C;\n",
              "    box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "    filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "    fill: #FFFFFF;\n",
              "  }\n",
              "</style>\n",
              "\n",
              "    <script>\n",
              "      async function quickchart(key) {\n",
              "        const containerElement = document.querySelector('#' + key);\n",
              "        const charts = await google.colab.kernel.invokeFunction(\n",
              "            'suggestCharts', [key], {});\n",
              "      }\n",
              "    </script>\n",
              "\n",
              "      <script>\n",
              "\n",
              "function displayQuickchartButton(domScope) {\n",
              "  let quickchartButtonEl =\n",
              "    domScope.querySelector('#df-bf574b15-141a-4290-9a51-75b2c2a2c150 button.colab-df-quickchart');\n",
              "  quickchartButtonEl.style.display =\n",
              "    google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "}\n",
              "\n",
              "        displayQuickchartButton(document);\n",
              "      </script>\n",
              "      <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-119e686d-3a1c-4f9f-8f79-d45ba1e6118f button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-119e686d-3a1c-4f9f-8f79-d45ba1e6118f');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n"
            ],
            "text/plain": [
              "            id      vector\n",
              "0       Wall-E  [1.0, 1.0]\n",
              "2  Ratatouille  [3.0, 3.0]"
            ]
          },
          "execution_count": 8,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "romantic_comedies = [\"Wall-E\", \"Ratatouille\"]\n",
        "romcom_df = df[df.id.isin(romantic_comedies)]\n",
        "romcom_df"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "registered-eagle",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-16T15:12:44.686462Z",
          "iopub.status.busy": "2021-04-16T15:12:44.685559Z",
          "iopub.status.idle": "2021-04-16T15:12:44.891495Z",
          "shell.execute_reply": "2021-04-16T15:12:44.890307Z"
        },
        "id": "registered-eagle",
        "outputId": "6a57ed6b-43cb-49f0-b85e-917e4a5869d2",
        "papermill": {
          "duration": 0.244898,
          "end_time": "2021-04-16T15:12:44.891970",
          "exception": false,
          "start_time": "2021-04-16T15:12:44.647072",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'dimension': 2,\n",
              " 'index_fullness': 0.0,\n",
              " 'namespaces': {'': {'vector_count': 4},\n",
              "                'romantic-comedy': {'vector_count': 2}},\n",
              " 'total_vector_count': 6}"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Insert vectors into a namespace\n",
        "index.upsert(vectors=zip(romcom_df.id, romcom_df.vector), namespace=\"romantic-comedy\")\n",
        "index.describe_index_stats()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "eight-sixth",
      "metadata": {
        "id": "eight-sixth",
        "papermill": {
          "duration": 0.032846,
          "end_time": "2021-04-16T15:12:44.963008",
          "exception": false,
          "start_time": "2021-04-16T15:12:44.930162",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "### Query top-3 results, without a namespace"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "id": "timely-allen",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-16T15:12:45.035032Z",
          "iopub.status.busy": "2021-04-16T15:12:45.034363Z",
          "iopub.status.idle": "2021-04-16T15:12:45.147905Z",
          "shell.execute_reply": "2021-04-16T15:12:45.146636Z"
        },
        "id": "timely-allen",
        "outputId": "3b1d65b7-4809-4c5e-db23-1a44d2fc8079",
        "papermill": {
          "duration": 0.152593,
          "end_time": "2021-04-16T15:12:45.148326",
          "exception": false,
          "start_time": "2021-04-16T15:12:44.995733",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'matches': [{'id': 'Wall-E', 'score': 0.0, 'values': []},\n",
              "             {'id': 'Up', 'score': 1.99999905, 'values': []},\n",
              "             {'id': 'Ratatouille', 'score': 7.99999809, 'values': []}],\n",
              " 'namespace': ''}"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "query_results = index.query(vector=df[df.id == \"Wall-E\"].vector[0], top_k=3)\n",
        "query_results"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "arabic-shooting",
      "metadata": {
        "id": "arabic-shooting",
        "papermill": {
          "duration": 0.034931,
          "end_time": "2021-04-16T15:12:45.223865",
          "exception": false,
          "start_time": "2021-04-16T15:12:45.188934",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "### Query top-3 results, with a namespace\n",
        "\n",
        "We should expect to see only romantic comedies in the query results."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "id": "stuck-hardware",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "execution": {
          "iopub.execute_input": "2021-04-16T15:12:45.302733Z",
          "iopub.status.busy": "2021-04-16T15:12:45.300355Z",
          "iopub.status.idle": "2021-04-16T15:12:45.411744Z",
          "shell.execute_reply": "2021-04-16T15:12:45.410749Z"
        },
        "id": "stuck-hardware",
        "outputId": "fe37ce47-ace9-4835-9c2a-5f245ddfb441",
        "papermill": {
          "duration": 0.151954,
          "end_time": "2021-04-16T15:12:45.412130",
          "exception": false,
          "start_time": "2021-04-16T15:12:45.260176",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'matches': [{'id': 'Wall-E', 'score': 0.0, 'values': []},\n",
              "             {'id': 'Ratatouille', 'score': 7.99999809, 'values': []}],\n",
              " 'namespace': 'romantic-comedy'}"
            ]
          },
          "execution_count": 11,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "query_results = index.query(vector=\n",
        "    df[df.id == \"Wall-E\"].vector[0], top_k=3, namespace=\"romantic-comedy\"\n",
        ")\n",
        "query_results"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "geological-competition",
      "metadata": {
        "id": "geological-competition",
        "papermill": {
          "duration": 0.035328,
          "end_time": "2021-04-16T15:12:45.490265",
          "exception": false,
          "start_time": "2021-04-16T15:12:45.454937",
          "status": "completed"
        },
        "tags": []
      },
      "source": [
        "### Delete the index"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "id": "valuable-rehabilitation",
      "metadata": {
        "execution": {
          "iopub.execute_input": "2021-04-16T15:12:45.567258Z",
          "iopub.status.busy": "2021-04-16T15:12:45.566027Z",
          "iopub.status.idle": "2021-04-16T15:12:58.139410Z",
          "shell.execute_reply": "2021-04-16T15:12:58.138501Z"
        },
        "id": "valuable-rehabilitation",
        "papermill": {
          "duration": 12.613954,
          "end_time": "2021-04-16T15:12:58.139886",
          "exception": false,
          "start_time": "2021-04-16T15:12:45.525932",
          "status": "completed"
        },
        "tags": []
      },
      "outputs": [],
      "source": [
        "# delete the index\n",
        "pc.delete_index(index_name)"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.9"
    },
    "papermill": {
      "default_parameters": {},
      "duration": 51.37705,
      "end_time": "2021-04-16T15:12:58.702015",
      "environment_variables": {},
      "exception": null,
      "input_path": "/notebooks/quick_tour/namespacing.ipynb",
      "output_path": "/notebooks/tmp/quick_tour/namespacing.ipynb",
      "parameters": {},
      "start_time": "2021-04-16T15:12:07.324965",
      "version": "2.3.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
